﻿
<!--#include file="includes/database.asp" -->
<!--#include file="includes/global_varaiable.asp" -->
<!--#include file="giave_info.asp" -->

<%
Response.expires = 0
Response.expiresabsolute = Now() - 1
Response.addHeader "pragma", "no-cache"
Response.addHeader "cache-control", "private"
Response.addHeader "cache-control", "no-cache"
Response.addHeader "cache-control", "no-store"
Response.CacheControl = "no-cache"

Response.Buffer = True

Const ewSessionMessage = "Message"

' Load key from QueryString
x_GiaveID = Request.QueryString("GiaveID")

' Get action
sAction = Request.Form("a_edit")
If sAction = "" Or IsNull(sAction) Then
	sAction = "I"	' Display with input box
Else

	' Get fields from form
	x_GiaveID = Request.Form("x_GiaveID")
	x_Dongia = Request.Form("x_Dongia")
	
End If

' Check if valid key
'If x_GiaveID = "" Or IsNull(x_GiaveID) Then Response.Redirect "giave_list.asp"

Select Case sAction
	Case "I": ' Get a record to display
		If Not LoadData() Then ' Load Record based on key
			Session(ewSessionMessage) = "Không có dữ liệu"
			adoCon.Close ' Close adoConection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "giave_list.asp"
		End If
	Case "U": ' Update
		If EditData() Then ' Update Record based on key
			Session(ewSessionMessage) = "Cập nhật thành công"
			adoCon.Close ' Close adoConection
			Set adoCon = Nothing
			Response.Clear
			Response.Redirect "giave_list.asp"
		End If
End Select
%>
<!--#include file="header.asp"-->

<script type="text/javascript" src="ew.js"></script>

<script type="text/javascript">
<!--
function EW_checkMyForm(EW_this) {
if (EW_this.x_Dongia && !EW_checknumber(EW_this.x_Dongia.value)) {
	if (!EW_onError(EW_this, EW_this.x_Dongia, "TEXT", "Price phải là số"))
		return false; 
}
return true;
}
//-->
</script>

<p><span class="aspmaker">Edit TABLE: GIAVE<br><br><a href="giave_list.asp">Back to List</a></span></p>
<form name="giave_edit" id="giave_edit" action="giave_edit.asp" method="post" onSubmit="return EW_checkMyForm(this);">
<p>
<input type="hidden" name="a_edit" value="U">
<table class="ewTable">
	<!-- <input type="hidden" id="x_GiaveID" name="x_GiaveID" value="<%= x_GiaveID %>"> -->
	
	<tr id="r_GiaveID">
		<td class="ewTableHeader"><span>Price ID</span></td>
		<td class="ewTableAltRow"><span id="cb_x_GiaveID">
		<input type="text" name="x_GiaveID" id="x_GiaveID" size="30" disabled value="<%= x_GiaveID %>">
		</span></td>
	</tr>
	
	<tr id="r_Dongia">
		<td class="ewTableHeader"><span>Price</span></td>
		<td class="ewTableAltRow"><span id="cb_x_Dongia">
		<input type="text" name="x_Dongia" id="x_Dongia" size="30" value="<%= Session("Price") %>">
		</span></td>
	</tr>
</table>
<p>
<input type="submit" name="btnAction" id="btnAction" value="EDIT">
</form>
<!--#include file="footer.asp"-->
<%
adoCon.Close ' Close adoConection
Set adoCon = Nothing
%>
<%

'-------------------------------------------------------------------------------
' Function LoadData
' - Load Data based on Key Value
' - Variables setup: field variables

Function LoadData()
	
	Dim rs, sSql, sFilter
	sFilter = ewSqlKeyWhere
	sFilter = Replace(sFilter, "@GiaveID", AdjustSql(x_GiaveID)) ' Replace key value
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	
	Set rs = Server.CreateObject("ADODB.Recordset")
	
	rs.Open sSql, adoCon
	If rs.Eof Then
		LoadData = False
	Else
		LoadData = True
		rs.MoveFirst

		' Get the field contents
		x_GiaveID = rs("PriceID")
		x_Dongia = rs("Price")
		
		Session("PriceID") = x_GiaveID
		Session("Price") = x_Dongia
		
	End If
	rs.Close
	Set rs = Nothing
End Function
%>
<%

'-------------------------------------------------------------------------------
' Function EditData
' - Edit Data based on Key Value
' - Variables used: field variables

Function EditData()
	Dim rs, sSql, sFilter
	sFilter = ewSqlKeyWhere
	sFilter = Replace(sFilter, "@GiaveID", AdjustSql(Session("PriceID"))) ' Replace key value
	sSql = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, sFilter, "")
	Set rs = Server.CreateObject("ADODB.Recordset")
	rs.CursorLocation = 3
	
	'response.write(Session("PriceID"))
	
	rs.Open sSql, adoCon, 1, 2
	
	' clone old and new rs object
	Dim rsold, rsnew
	Set rsold = rs.Clone(1)
	rsold.Requery()
	Set rsnew = rs.Clone(1)
	If rs.Eof Then
		EditData = False ' Update Failed
	Else

		' Field Dongia
		sTmp = x_Dongia
		If Not IsNumeric(sTmp) Then
			sTmp = Null
		Else
			sTmp = cDbl(sTmp)
		End If
		rs("Price") = sTmp
		
		response.write(sTmp)

		' Call updating event
		If Recordset_Updating(rs, rsnew) Then
			rs.Update
			EditData = True
		Else
			rs.CancelUpdate
			EditData = False
		End If
	End If

	' Call updated event
	If EditData Then
		Call Recordset_Updated(rsold, rsnew)
	End If
	rs.Close
	Set rs = Nothing
	rsold.Close
	Set rsold = Nothing
	rsnew.Close
	Set rsnew = Nothing
End Function

'-------------------------------------------------------------------------------
' Recordset updating event

Function Recordset_Updating(rsold, rsnew)
	On Error Resume Next

	' Please enter your customized codes here
	Recordset_Updating = True
End Function

'-------------------------------------------------------------------------------
' Recordset updated event

Sub Recordset_Updated(rsold, rsnew)
	On Error Resume Next
End Sub
%>
